package com.bjsxt;
/**守护线程*/

class Daemon01 implements Runnable{
    @Override
    public void run() {
        for (int i=0;i<20;i++){
            System.out.println(Thread.currentThread().getName()+" "+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class UserDaemon01 implements Runnable{
    @Override
    public void run() {
        //将Daemon线程设置为UserDaemon守护线程
        //守护线程会随着用户线程死亡而死亡
        Thread t2 = new Thread(new Daemon(),"Daemon");
        t2.setDaemon(true);
        t2.start();
        for (int i=0;i<5;i++){
            System.out.println(Thread.currentThread().getName()+" "+i);
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public class DaemonThread01 {
    public static void main(String[] args) throws Exception{
        Thread t = new Thread(new UserDaemon01());
        t.start();
        Thread.sleep(1000);
        System.out.println("主线程结束");
    }
}
